/**
 * @file 异步任务预加载API
 * @description 为渲染进程提供异步任务相关的API接口
 * @author AI Assistant
 * @version 1.0.0
 * @since 2025-10-30
 */

import type {
  AsyncTask,
  CreateAsyncTaskInput,
  DatabaseResult,
  QueryAsyncTaskParams,
  UpdateAsyncTaskInput
} from '~types/async-task'
// @ts-ignore
const electron = require('electron')

// IPC 通道常量
const ASYNC_TASK_CHANNELS = {
  CREATE_TASK: 'async-task:create-task',
  DELETE_TASK: 'async-task:delete-task',
  DELETE_TASKS: 'async-task:delete-tasks',
  CLEAR_ALL_TASKS: 'async-task:clear-all-tasks',
  FIND_BY_ID: 'async-task:find-by-id',
  QUERY_TASKS: 'async-task:query-tasks',
  GET_PENDING_TASKS: 'async-task:get-pending-tasks',
  GET_IN_PROGRESS_TASKS: 'async-task:get-in-progress-tasks',
  GET_COMPLETED_TASKS: 'async-task:get-completed-tasks',
  GET_FAILED_TASKS: 'async-task:get-failed-tasks',
  GET_TASKS_BY_TYPE: 'async-task:get-tasks-by-type',
  GET_TASK_COUNT: 'async-task:get-task-count',
  UPDATE_TASK: 'async-task:update-task',
  UPDATE_PROGRESS: 'async-task:update-progress',
  MARK_AS_IN_PROGRESS: 'async-task:mark-as-in-progress',
  MARK_AS_COMPLETED: 'async-task:mark-as-completed',
  MARK_AS_FAILED: 'async-task:mark-as-failed',
  CLEANUP_COMPLETED_TASKS: 'async-task:cleanup-completed-tasks',
  CLEANUP_FAILED_TASKS: 'async-task:cleanup-failed-tasks'
} as const

/**
 * 异步任务API对象
 */
export const asyncTask = {
  // ==================== 创建操作 ====================

  /**
   * 创建异步任务
   */
  createTask: async (input: CreateAsyncTaskInput): Promise<DatabaseResult<AsyncTask>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.CREATE_TASK, input)
  },

  // ==================== 删除操作 ====================

  /**
   * 删除异步任务
   */
  deleteTask: async (id: number): Promise<DatabaseResult<void>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.DELETE_TASK, id)
  },

  /**
   * 批量删除异步任务
   */
  deleteTasks: async (ids: number[]): Promise<DatabaseResult<void>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.DELETE_TASKS, ids)
  },

  /**
   * 清空所有异步任务
   */
  clearAllTasks: async (): Promise<DatabaseResult<void>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.CLEAR_ALL_TASKS)
  },

  // ==================== 查询操作 ====================

  /**
   * 根据ID查找异步任务
   */
  findById: async (id: number): Promise<DatabaseResult<AsyncTask | null>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.FIND_BY_ID, id)
  },

  /**
   * 查询异步任务
   */
  queryTasks: async (params: QueryAsyncTaskParams): Promise<DatabaseResult<AsyncTask[]>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.QUERY_TASKS, params)
  },

  /**
   * 获取待处理的任务
   */
  getPendingTasks: async (limit?: number): Promise<DatabaseResult<AsyncTask[]>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.GET_PENDING_TASKS, limit)
  },

  /**
   * 获取进行中的任务
   */
  getInProgressTasks: async (limit?: number): Promise<DatabaseResult<AsyncTask[]>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.GET_IN_PROGRESS_TASKS, limit)
  },

  /**
   * 获取已完成的任务
   */
  getCompletedTasks: async (limit?: number): Promise<DatabaseResult<AsyncTask[]>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.GET_COMPLETED_TASKS, limit)
  },

  /**
   * 获取失败的任务
   */
  getFailedTasks: async (limit?: number): Promise<DatabaseResult<AsyncTask[]>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.GET_FAILED_TASKS, limit)
  },

  /**
   * 根据类型获取任务
   */
  getTasksByType: async (
    taskType: string,
    limit?: number
  ): Promise<DatabaseResult<AsyncTask[]>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.GET_TASKS_BY_TYPE, taskType, limit)
  },

  /**
   * 获取任务总数
   */
  getTaskCount: async (): Promise<DatabaseResult<number>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.GET_TASK_COUNT)
  },

  // ==================== 更新操作 ====================

  /**
   * 更新异步任务
   */
  updateTask: async (input: UpdateAsyncTaskInput): Promise<DatabaseResult<AsyncTask>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.UPDATE_TASK, input)
  },

  /**
   * 更新任务进度
   */
  updateProgress: async (id: number, progress: number): Promise<DatabaseResult<AsyncTask>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.UPDATE_PROGRESS, id, progress)
  },

  /**
   * 标记任务为进行中
   */
  markAsInProgress: async (id: number): Promise<DatabaseResult<AsyncTask>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.MARK_AS_IN_PROGRESS, id)
  },

  /**
   * 标记任务为已完成
   */
  markAsCompleted: async (
    id: number,
    resultData?: Record<string, any>
  ): Promise<DatabaseResult<AsyncTask>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.MARK_AS_COMPLETED, id, resultData)
  },

  /**
   * 标记任务为失败
   */
  markAsFailed: async (id: number, errorMessage: string): Promise<DatabaseResult<AsyncTask>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.MARK_AS_FAILED, id, errorMessage)
  },

  // ==================== 清理操作 ====================

  /**
   * 清理已完成的任务
   */
  cleanupCompletedTasks: async (olderThanDays?: number): Promise<DatabaseResult<void>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.CLEANUP_COMPLETED_TASKS, olderThanDays)
  },

  /**
   * 清理失败的任务
   */
  cleanupFailedTasks: async (olderThanDays?: number): Promise<DatabaseResult<void>> => {
    return electron.ipcRenderer.invoke(ASYNC_TASK_CHANNELS.CLEANUP_FAILED_TASKS, olderThanDays)
  }
}
